﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace RkLibs.Configuration
{
    public class ConfigIni:IConfiguration
    {
        [DllImport("kernel32.dll")]
        private static extern bool WritePrivateProfileSection(string lpAppName, string lpString, string lpFileName);

        [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);

        [DllImport("kernel32.dll")]
        private static extern bool WritePrivateProfileStruct(string lpszSection, string lpszKey, IntPtr lpStruct, uint uSizeStruct, string szFile);

        [DllImport("kernel32.dll")]
        private static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault, string lpFileName);

        [DllImport("kernel32.dll")]
        private static extern uint GetPrivateProfileSection(string lpAppName, IntPtr lpReturnedString, uint nSize, string lpFileName);

        [DllImport("kernel32.dll")]
        private static extern uint GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, uint nSize, string lpFileName);

        [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
        private static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, uint nSize, string lpFileName);

        [DllImport("kernel32.dll")]
        private static extern bool GetPrivateProfileStruct(string lpszSection, string lpszKey, IntPtr lpStruct, uint uSizeStruct, string szFile);

        /// <summary>
        /// 最大字符串长度
        /// </summary>
        private const int UCHAR_MAX = byte.MaxValue;
        /// <summary>
        /// 配置文件全路径
        /// </summary>
        private string _ConfigFilePath;
        public string ConfigFilePath
        {
            get
            {
                return _ConfigFilePath;
            }
            set
            {
                _ConfigFilePath = value;
            }
        }

        public ConfigIni(string filePath)
        {
            ConfigFilePath = filePath;
        }


        public bool WriteConfig(string section, string key, object value)
        {
            bool nRet = WritePrivateProfileString(section, key, value.ToString(), _ConfigFilePath);
            return nRet;
        }
        public bool ReadBool(string section, string key, bool def = true)
        {
            string strTmp;
            strTmp = ReadString(section, key, def.ToString());

            try
            {
                return Convert.ToBoolean(strTmp);
            }
            catch
            {

            }

            return def;
        }

        public int ReadInt(string section, string key, int def = 0)
        {
            int nRet = GetPrivateProfileInt(section, key, def, _ConfigFilePath);
            return nRet;
        }

        public string ReadString(string section, string key, string def = "")
        {
            string Ret;
            StringBuilder sb = new StringBuilder(UCHAR_MAX);
            GetPrivateProfileString(section, key, def, sb, UCHAR_MAX, _ConfigFilePath);
            Ret = sb.ToString();
            sb = null;
            return Ret;
        }

        public bool DeleteSection(string section)
        {
            bool nRet = WritePrivateProfileString(section, null, "", _ConfigFilePath);
            return nRet;
        }
        public bool DeleteKey(string section, string key)
        {
            bool nRet = WritePrivateProfileString(section, key, null, _ConfigFilePath);
            return nRet;
        }
    }
}
